﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;

namespace Utils
{
    public class CheckValidate
    {
        public static bool IsValidNumberString(string numberstring)
        {
            numberstring = numberstring.Trim();
            //System.Text.RegularExpressions.Regex.IsMatch(numberstring, "/[^a-z]/i");
            if (System.Text.RegularExpressions.Regex.IsMatch(numberstring, "/[^a-z]/i"))
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        // Function to test whether the string is valid number or not
        public static bool IsNumber(String strNumber)
        {
            Regex objNotNumberPattern = new Regex("[^0-9.-]");
            Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
            Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*");
            String strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
            String strValidIntegerPattern = "^([-]|[0-9])[0-9]*$";
            Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")");
            return !objNotNumberPattern.IsMatch(strNumber) &&
            !objTwoDotPattern.IsMatch(strNumber) &&
            !objTwoMinusPattern.IsMatch(strNumber) &&
            objNumberPattern.IsMatch(strNumber);
        }

        // Function to Test for Integers both Positive & Negative 
        public static bool IsInteger(String strNumber)
        {
            Regex objNotIntPattern = new Regex("[^0-9-]");
            Regex objIntPattern = new Regex("^-[0-9]+$|^[0-9]+$");
            return !objNotIntPattern.IsMatch(strNumber) && objIntPattern.IsMatch(strNumber);
        }

        public static string TrueFalseConverter(bool b, string trueVal, string falseVal)
        {
            return b ? trueVal : falseVal;
        }

        public static int ToInt32(string p)
        {
            if (IsNumber(p) && IsInteger(p))
            {
                return Convert.ToInt32(p);
            }
            else
            {
                return 0;
            }
        }

        public static DateTime VnTimeToUsTime(string time)
        {
            IFormatProvider format = new CultureInfo("vi-VN");
            return DateTime.Parse(time, format);
        }


        public static bool CheckNull(string str)
        {
            return (str.Length == 0 || str.Equals(null) || str.ToString() == "");
        }
        public static bool CheckLength(string str, int max, int min)
        {
            return (str.Length > max || str.Length < min);
        }
        public static int CheckLengthNumber(string str)
        {
            return str.Length;
        }
        public static bool CheckEmail(string str)
        {
            string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
            @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
            @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
            Regex re = new Regex(strRegex);
            return re.IsMatch(str);
        }

        public static bool CheckAlphabetNumber(string str)
        {
            Regex invalidCharsRegex = new Regex(@"[\*\?\|\\\t/:""<>#{}%~]",
                RegexOptions.Compiled);
            return invalidCharsRegex.IsMatch(str);
        }

        public static bool CheckMoney(string str)
        {
            Regex invalidCharsRegex = new Regex(@"[,]",
                RegexOptions.Compiled);
            return invalidCharsRegex.IsMatch(str);
        }

        public static string RemoveSpecialChars(string str)
        {
            string[] chars = new string[] { ",", ".", "/", "!", "@", "#", "$", "%", "^", "&", "*", "'", "\"", ";", "-", "_", "(", ")", ":", "|", "[", "]" };
            //string[] chars = new string[] { "@", "#", "$", "%", "^", "&", "*", "'" };
            for (int i = 0; i < chars.Length; i++)
            {
                if (str.Contains(chars[i]))
                {
                    if (char.Parse(chars[i]) == '&')
                        str = str.Replace(chars[i], "+");
                    if (char.Parse(chars[i]) == '\'')
                        str = str.Replace(chars[i], "");
                    if (char.Parse(chars[i]) == '-')
                        str = str.Replace(chars[i], "");
                    else
                        str = str.Replace(chars[i], "");
                }
            }
            return str;
        }

        public static bool CheckMinus(string num)
        {
            return num.StartsWith("-") || num.StartsWith("_") || num.StartsWith(" ");
        }

        public static bool IsValidNumber(string str)
        {
            bool bVal = false;
            string[] sValue = str.Split('.');
            try
            {
                for (int nlen = 0; nlen < sValue.Length; nlen++)
                {
                    if (int.Parse(sValue[nlen].ToString()) > 0)
                    {
                        bVal = true;
                    }
                    else if (int.Parse(sValue[nlen].ToString()) > 0)
                    {
                        return false;
                    }
                }
            }
            catch (Exception)
            {
                bVal = false;
            }
            return bVal;
        }

        public static bool IsNumeric(string strNumber)
        {
            Regex objNotPositivePattern = new Regex("[^0-9.]");
            Regex objPositivePattern = new Regex("^[.][0-9]+$|[0-9]*[.]*[0-9]+$");
            Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
            return !objNotPositivePattern.IsMatch(strNumber) &&
            objPositivePattern.IsMatch(strNumber) &&
            !objTwoDotPattern.IsMatch(strNumber);
        }

        public Boolean isValidPassword(string thisPassword)
        {
            string newPassword = sanitizeInput(thisPassword);
            Regex regX = new Regex(@"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{5,15)");
            return regX.Match(newPassword).Success;
        }

        public string sanitizeInput(string thisInput)
        {
            Regex regX = new Regex(@"([<>""'%;()&])");
            return regX.Replace(thisInput, "");
        }
    }
}
